/*
 *  SPAGL Copyright (C) 2008 Sam Pottinger
 *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
 *  General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
 *  (at your option) any later version. This library is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License along with this library; if not, 
 *  write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package spagswing;

import java.util.ArrayList;
import javax.swing.ButtonGroup;
import spagcore.SpaglCoreContainer;
import spagcore.SpaglCoreEvent;
import spagcore.SpaglCoreRadioButton;
import spagcore.SpaglCoreRadioGroup;
import spagcore.SpaglCoreWidget;

/**
 * A ButtonGroup (javax.swing.ButtonGroup) that conforms to the SpaglCoreRadioGroup interface
 * @author Sam Pottinger
 */
public class SpaglRadioGroup extends ButtonGroup implements SpaglCoreRadioGroup, SpaglCoreContainer {

	private static final long serialVersionUID = 1L;
	private ArrayList<SpaglRadioButton> buttons = new ArrayList<SpaglRadioButton>();
	private SpaglCoreContainer parentWidget;
	private SpaglCoreContainer myContainer;
	private boolean eventSpecified = false;
	private ArrayList<SpaglEventListener> events = new ArrayList<SpaglEventListener>(0);

	/**
	 * Creates a bare-bones radio group with the default placement behavior
	 * @param newParent The container that will hold this widget
	 */
	public SpaglRadioGroup(SpaglCoreContainer newParent)
	{
		super();
		parentWidget = newParent;
	}
	
	/**
	 * Creates a radio group with the specified placement parameters
	 * @param newParent The container that will hold this widget
	 * @param placementParameters The string placement parameters that will be used with the layout manager
	 */
	public SpaglRadioGroup(SpaglCoreContainer newParent, String placementParameters)
	{
		super();
		parentWidget = newParent;
		parentWidget.placeWidget((SpaglContainer)myContainer, placementParameters);
	}
	
	/**
	 * Adds a new radio button to this widget using the default layout settings
	 * @param newButton The radio button (that conforms to SpaglCoreRadioButton) to be added
	 * @return The button (as a SpaglCoreRadioButton) that was just added
	 */
	public SpaglCoreRadioButton addButton(SpaglCoreRadioButton newButton) {
		super.add((SpaglRadioButton)newButton);
		buttons.add((SpaglRadioButton)newButton);
		if (eventSpecified) addEventsToButton((SpaglRadioButton)newButton);
		return newButton;
	}
	
	/**
	 * Adds a new radio button to this widget with the specified layout settings
	 * @param newButton The radio button (that conforms to SpaglCoreRadioButton) to be added
	 * @param placementParameters The string parameters to be used to place this widget
	 * @return The button (as a SpaglCoreRadioButton) that was just added
	 */
	public SpaglCoreRadioButton addButton(SpaglCoreRadioButton newButton, String placementParameters) {
		super.add((SpaglRadioButton)newButton);
		buttons.add((SpaglRadioButton)newButton);
		myContainer.placeWidget(newButton, placementParameters);
		if (eventSpecified) addEventsToButton((SpaglRadioButton)newButton);
		return newButton;
	}

	/**
	 * Creates a new SpaglRadioButton with the given text and uses the layout manager's default behavior
	 * @param newText The text of the new radio button
	 * @return The button (as a SpaglCoreRadioButton) that was just added
	 */
	public SpaglCoreRadioButton addButton(String newText) {
		SpaglCoreRadioButton newButton = new SpaglRadioButton(myContainer, newText);
		super.add((SpaglRadioButton)newButton);
		buttons.add((SpaglRadioButton)newButton);
		if (eventSpecified) addEventsToButton((SpaglRadioButton)newButton);
		return newButton;
	}

	/**
	 * Creates a new SpaglRadioButton with the given text and uses the specified parameters to place this widget
	 * @param newText The text of the new radio button
	 * @param placementParameters The string parameters to be used to place this widget
	 * @return The button (as a SpaglCoreRadioButton) that was just added
	 */
	public SpaglCoreRadioButton addButton(String newText, String placementParameters) {
		SpaglCoreRadioButton newButton = new SpaglRadioButton(myContainer, newText);
		super.add((SpaglRadioButton)newButton);
		myContainer.placeWidget(newButton, placementParameters);
		buttons.add((SpaglRadioButton)newButton);
		if (eventSpecified) addEventsToButton((SpaglRadioButton)newButton);
		return newButton;
	}
	
	/**
	 * Gets an array of radio buttons associated with this widget
	 * @return An array of SpaglCoreRadioButtons that are inside this widget
	 */
	public SpaglCoreRadioButton[] getButtons() {
		return (SpaglCoreRadioButton[])buttons.toArray();
	}

	/**
	 * Removes the button belonging to this widget at given index
	 * @param i The index of the button to be removed
	 */
	public void removeButton(int i) {
		super.remove(buttons.get(i));
		buttons.remove(i);
	}

	/**
	 * Sets the selected radio button
	 * @param newSelection The index of the radio button to be selected
	 */
	public void setSelection(int newSelection) {
		for(int i = 0; i < buttons.size(); i++)
		{
			if(i == newSelection)
				buttons.get(i).setSelected(true);
			else
				buttons.get(i).setSelected(false);
		}
	}
	
	/**
	 * Sets the selected radio button
	 * @param newSelection The radio button object to be selected
	 */
	public void setSelection(SpaglCoreRadioButton newSelection) {
		for(int i = 0; i < buttons.size(); i++)
		{
			if(buttons.get(i) == newSelection)
				buttons.get(i).setSelected(true);
			else
				buttons.get(i).setSelected(false);
		}
	}

	/**
	 * Gets the index of the selected button
	 * @return The integer index of the selected button
	 */
	public int getSelectedIndex() {
		for(int i = 0; i < buttons.size(); i++)
		{
			if(buttons.get(i).isSelected())
				return i;
		}
		return -1;
	}
	
	/**
	 * Gets the SpaglCoreRadioButton object that is selected by the user
	 * @return The radio button selected as a SpaglCoreRadioButton
	 */
	public SpaglCoreRadioButton getSelectedButton()
	{
		for(int i = 0; i < buttons.size(); i++)
		{
			if(buttons.get(i).isSelected())
				return buttons.get(i);
		}
		return null;
	}

	/**
	 * Gets the container that is holding this widget
	 * @return The SpaglCoreContainer-compliant object that this widget is inside
	 */
	public SpaglCoreContainer getContainer() {
		return parentWidget;
	}

	/**
	 * Gets the vertical size of this widget
	 * @return An integer representing the absolute height of this object
	 */
	public int getHeight() {
		return myContainer.getHeight();
	}

	/**
	 * Gets the horizontal size of this widget
	 * @return An integer representing the absolute width of this object
	 */
	public int getWidth() {
		return myContainer.getWidth();
	}

	/**
	 * Sets the absolute size of this widget
	 * @param width the horizontal size of this widget
	 * @param height the vertical size of this widget
	 */
	public void setSize(int width, int height) {
		myContainer.setSize(width, height);
	}

	/**
	 * Places a widget on this container with the given string parameters
	 * @param w the target widget to be placed in this container
	 * @param param the string parameters for the layout manager
	 */
	public void placeWidget(SpaglCoreWidget w, String param) {
		myContainer.placeWidget(w, param);
	}
	
	/**
	 * Adds an event listener each radio button in this group
	 * 
	 * @param eventID The ID of the event to be listened for
	 * @param method The (string) name of the method to be called when the event is fired
	 * @param targetObject The object where the method should be fired 
	 */
	public void addEventListener(int eventID, String method, Object targetObject) {
		try{
			switch(eventID)
			{
				case SpaglEvent.SELECTION_CHANGED_EVENT: {distributeEvent(new SpaglEventListener(targetObject.getClass().getMethod(method, SpaglCoreEvent.class), targetObject, SpaglEvent.SELECTION_CHANGED_EVENT)); break;}
				default: throw new IllegalArgumentException("Event not supported by this widget");
			}}
		catch (SecurityException e) {e.printStackTrace();} 
		catch (NoSuchMethodException e) {e.printStackTrace();}
		
	}
	
	// Add the specified event listener to each of the buttons in this group and specify that an event has been choosen
	private void distributeEvent(SpaglEventListener listener) {
		for(SpaglRadioButton b : buttons)
			b.addActionListener(listener);
		eventSpecified = true;
		events.add(listener);
	}

	// Add the event listeners attached to this group to the specified button
	private void addEventsToButton(SpaglRadioButton newButton)
	{
		for(SpaglEventListener event : events)
			newButton.addActionListener(event);
	}
	
	/**
	 * Swing does not need manual disposal, so this is here just for cross library purposes
	 */ 
	public void dispose() {}
}
